/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.its.dsw2.view;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.its.dsw2.biblioteca.Autor;
import mx.its.dsw2.biblioteca.bl.AutorBlLocal;

/**
 *
 * @author Piter
 */
@ManagedBean
@SessionScoped
public class AuthorBean {
    //----------------------------llamadas alos ejb's------------------------------------------------

    @EJB
    private AutorBlLocal autorBl;
    //--------------------------------------------variables (propiedades)
    private List<Autor> lista;
    private Autor author;
    private String accion;

    /**
     * Creates a new instance of AuthorBean
     */
    public AuthorBean() {
        author = new Autor();
    }
    //------------------------------- metodos get y set----------------------------------

    public List<Autor> getLista() {
        lista = autorBl.getAll();
        return lista;
    }

    public Autor getAuthor() {
        return author;
    }

    public void setAuthor(Autor author) {
        this.author = author;
    }

//-------------------------------Acciones--------------------------
    public String procesarAccion() {
        if (accion.equalsIgnoreCase("Nuevo")) {
            autorBl.Register(author);
        } else if (accion.equalsIgnoreCase("Editar")) {
            autorBl.modify(author);
        }else if (accion.equalsIgnoreCase("Eliminar")){
             autorBl.delete(author);
        }

        accion = "";
        return "Autor";
    }

//--------------------------------listeners-------------------------
    public void preparNuevo(ActionEvent evt) {
        author = new Autor();
        accion="nuevo";
    }

    public void prepararEditar(ActionEvent evt) {
//        System.out.println(evt.getComponent().getClientId());
//        String arr[]= evt.getComponent().getClientId().split(":");
//        String id=arr[2];
//        int index=Integer.parseInt(id);
//        
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author = lista.get(index);
        accion="editar";
    }
    
    public void eliminar(ActionEvent evt){
        accion="eliminar";
        int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
        author = lista.get(index);
        
    }
    public String marrirris() {
        String h = "quiero ir a chapala";
        if (accion.equalsIgnoreCase("Nuevo")) {
            h = "Autor";
        } else if (accion.equalsIgnoreCase("Editar")) {
            h = "AutorCrearEditar";
        } else if (accion.equalsIgnoreCase("Eliminar")) {
            h = "Autor";
        }

        accion = "";
        return h;
    }
    
    
}